<!DOCTYPE html>
<html class="pixel-ratio-2 retina android android-6 android-6-0">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta name="format-detection" content="telephone=no" />
    <title>首页</title>
    <link rel="stylesheet" href="sx_assets/css/style.css">
	<!-- <link rel="stylesheet" href="sx_assets/css/mui.min.css"> -->
	<link rel="stylesheet" href="sx_assets/font/iconfont.css">
</head>
	<body>
		<div id="vue_det">
			<!-- 弹出层 -->
			<div class="mask" v-show="showMask" v-on:click="hidePop()"></div>
			<div id="popup" class="popup" v-show="popUp" >
				<div class="popup-tab">蓝牙设备列表</div>
				<div class="popup-content">
					<div class="popup-list" v-for="(ble,key) in bluetoothList" :key="key"  v-on:click="connectBluetooth(ble.mac)">
						<li class="popup-title">{{ble.name}}</li>
						<div class="popup-list-icon"><i class="iconfont">&#xe6a4;</i></div>
					</div>
					
				</div>
			</div>
			<!-- 标题栏 -->
			<div class="navigation-bar">
				<div class="navigation-left">
					<div class="navigation-left-title" v-on:click="selectCollect()">选择集中器：{{selCollect}}</div>
				</div>
				<div class="navigation-right">
					<div class="navigation-right-title fs16" v-on:click="searchBluetooth()">
						<i class="iconfont">{{blueStatus}}</i>
					</div>
				</div>
			</div>
			<!-- 内容 -->
			<div class="grid-content">
				<ul  id="menu_list">
					<li class="grid-view-item" v-for="(value,key) in menu" :key="key" v-on:click="goPage(value.url)">
						<div class="grid-view-item-ico colorA"><i class="iconfont">{{value.icon}}</i></div>
						<div class="grid-view-item-title">{{value.title}}</div>
					</li>
				</ul>
			</div>
			<!--  -->
		</div>
	</body>
</html>
<script src="sx_assets/js/vue.min.js"></script>
<script src="sx_assets/js/vconsole.min.js"></script>
<script>
	// 初始化
	var vConsole = new VConsole();
	console.log('init');
	document.addEventListener('PHJSReady', function() {
	        
	});
	// $(document).ready(function() {
	//     document.addEventListener('PHJSReady', function() {
	        
	//     });
	// });
	var vm = new Vue({
		el: '#vue_det',
		data: {
			menu:[
				{
					title:"终端管理",
					icon:"\ue649",
					url:"test.html"
				},
				{
					title:"电表档案",
					icon:"\ue614",
					url:"meter.html"
				},
				{
					title:"表箱管理",
					icon:"\ue60e",
					url:"box.html"
				},
				{
					title:"数据采集",
					icon:"\ue6da",
					url:"getData.html"
				},
				{
					title:"线损计算",
					icon:"\ue64c",
					url:"workOut.html"
				},
			],
			selCollect:'000000000000',
			blueStatus:'\ue642',
			bluetoothList:[],
			popUp:false,
			showMask:false,
		},
		created: function () {
			this.docReady();
		},
		methods: {
			docReady:function(){
				document.addEventListener('PHJSReady', function() {
				    
				});
			},
			hidePop:function(){
				this.showMask = false;
				this.popUp = false;
				this.stopSearchBluetooth();
			},
			goPage:function(url) {
				window.location.href = url;
			},
			openBluetooth: function() {
				PHBluetooth. openBluetooth();
			},
			searchBluetooth:function(){
				this.openBluetooth();//开启蓝牙
				PHLoading.show();
				var that = this;
				PHBluetooth.search(function(res){
					console.log(JSON.stringify(res));
					if(res.mac != '' && res.name != '' ){
						that.bluetoothList.push({
							mac:res.mac,
							name:res.name,
						})
					}
					//弹出蓝牙设备列表
					that.popUp = true;
					that.showMask = true;
				},function(res){
					console.log(JSON.stringify(res));
				})
				PHLoading.hide();
			},
			stopSearchBluetooth:function(){
				PHBluetooth. stopSearch();
			},
			connectBluetooth:function(mac){
				this.showMask = false;
				this.popUp = false;
				var that = this;
				PHBluetooth.connect(mac,function(res){
					that.blueStatus = '\uec4a';
					console.log(JSON.stringify(res));
					var len = res.service.length, charactersArry = [];
					for(let i=0;i<len;i++){
						if(res.service[i].characters[0].property == 30 ){
							charactersArry.push({
								macId:"B0:7E:11:DE:7B:26",
								serviceId:res.service[i].uuid,
								characteristicId:res.service[i].characters[0].uuid,
								descriptorId:res.service[i].characters[0].descriptors[0].uuid,
							})
						}
					}
					console.log("readwritenotify"+JSON.stringify(charactersArry));
					var buffer = new ArrayBuffer(16);
					var iv = new Uint8Array(buffer);
					var data = "68 93 02 12 06 19 20 68 11 04 3F 34 33 37 A8 16";
					var val = data.split(" ");
					console.log(val.length);
					for(let i=0;i<val.length;i++){
						iv[i] = "0x"+val[i];
					}
					var macId = charactersArry[0].macId;
					var serviceId = charactersArry[0].serviceId;
					var characteristicId = charactersArry[0].characteristicId;
					var descriptorId = charactersArry[0].descriptorId;
					//写入
					PHBluetooth. writeDescriptor(macId, serviceId, characteristicId, descriptorId, buffer, function(res){
						console.log("write success::" + JSON.stringify(res));
						//回调
						PHBluetooth. notify(macId, serviceId, characteristicId, function(res){
							console.log("notify success::" + JSON.stringify(res));
							//读取描述
							PHBluetooth. readDescriptor(macId, serviceId, characteristicId, descriptorId, function(res){
								console.log("read success::" + JSON.stringify(res));
								var value = that.buffer2hex(res.rssi);
								let data = "";
								let len = value.length;
								for (let i = 0; i < len; i += 2) {
									data += value.substring(i, (i + 2) > len ? len : (i + 2)) + " ";
								}
								console.log('value(hex) = '+data);
							}, function(res){
								console.log("read fail::" + JSON.stringify(res));
							})
						}, function(res){
							console.log("notify fail::" + JSON.stringify(res));
						})
					}, function(res){
						console.log("write fail::" + JSON.stringify(res));
					})
				},function(res){
					console.log("connect fail::" + JSON.stringify(res));
				})
				this.stopSearchBluetooth();
			},
			selectCollect:function(){
				console.log("selectCollect");
			},
			buffer2hex:function(buffer){
				return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
			}
		}
	})
	 
</script>
